package controllers

import (
	"github.com/gin-gonic/gin"
	"go.didapinche.com/foundation/apisix-dashboard/internal/app/models"
	"go.didapinche.com/foundation/apisix-dashboard/internal/app/services"
	"net/http"
)

type UpstreamController struct {
	service services.UpstreamService
}

func NewUpstreamController(service services.UpstreamService) *UpstreamController {
	return &UpstreamController{service: service}
}

func (ctl UpstreamController) Get(c *gin.Context) {
	env, exist := c.Get("env")
	if !exist {
		c.String(http.StatusForbidden, "env not exist")
	}
	key := c.Param("id")
	u, err := ctl.service.Get(key, env.(string))
	if err != nil {
		c.String(http.StatusBadRequest, "UpstreamService Get failed:%v", err)
	}
	c.JSON(http.StatusOK, u)
}

func (ctl UpstreamController) GetName(c *gin.Context) {
	env, exist := c.Get("env")
	if !exist {
		c.String(http.StatusForbidden, "env not exist")
	}
	name := c.Query("name")
	u, err := ctl.service.GetName(name, env.(string))
	if err != nil {
		c.String(http.StatusBadRequest, "UpstreamService GetName failed:%v", err)
	}
	c.JSON(http.StatusOK, u)
}

func (ctl UpstreamController) List(c *gin.Context) {
	env, exist := c.Get("env")
	if !exist {
		c.String(http.StatusForbidden, "env not exist")
	}
	name := c.Query("name")
	us, err := ctl.service.List(name, env.(string))
	if err != nil {
		c.String(http.StatusBadRequest, "UpstreamService List failed:%v", err)
	}
	c.JSON(http.StatusOK, us)
}

func (ctl UpstreamController) Create(c *gin.Context) {
	env, exist := c.Get("env")
	if !exist {
		c.String(http.StatusForbidden, "env not exist")
	}
	param := new(models.Upstream)
	if err := c.ShouldBind(param); err != nil {
		c.String(http.StatusBadRequest, "bind params error:%v", err)
		return
	}
	err := ctl.service.Create(param, env.(string))
	if err != nil {
		c.String(http.StatusBadRequest, "UpstreamService Create failed:%v", err)
	}
}
func (ctl UpstreamController) Update(c *gin.Context) {
	env, exist := c.Get("env")
	if !exist {
		c.String(http.StatusForbidden, "env not exist")
	}
	id := c.Param("id")
	param := new(models.Upstream)
	if err := c.ShouldBind(param); err != nil {
		c.String(http.StatusBadRequest, "bind params error:%v", err)
		return
	}
	param.ID = id
	err := ctl.service.Update(param, env.(string))
	if err != nil {
		c.String(http.StatusBadRequest, "UpstreamService Update failed:%v", err)
	}
}

func (ctl UpstreamController) Delete(c *gin.Context) {
	env, exist := c.Get("env")
	if !exist {
		c.String(http.StatusForbidden, "env not exist")
	}
	key := c.Param("id")
	err := ctl.service.Delete(key, env.(string))
	if err != nil {
		c.String(http.StatusBadRequest, "UpstreamService Delete failed:%v", err)
	}
	c.JSON(http.StatusOK, "")
}

func (ctl UpstreamController) Syn(c *gin.Context) {
	param := new(struct {
		Upstream *models.Upstream `json:"upstream"`
		Env      []string         `json:"env"`
	})
	err := ctl.service.Syn(param.Upstream, param.Env)
	if err != nil {
		c.String(http.StatusBadRequest, "UpstreamService Syn failed:%v", err)
	}
	c.JSON(http.StatusOK, "")
}
